import { Nodes } from '@knno/dom';
import { Table as UITable } from '../components/table/table';
import { JSXProps } from '@knno/dom/jsx';
import { Column, ColumnSizeType, GridEventMap, RowType, Section, SortType } from '../components/grid/gridBase';
import { AttrCheck, isArray, isBool, isNumber, isObject, isStr, jsxBuild, On } from './utils';

export { UITable };

interface TableProps extends JSXProps<UITable>, On<GridEventMap, UITable> {
	columnBorder?: boolean;
	columnSize?: ColumnSizeType;
	columns?: Column[];
	headLine?: boolean;
	sortType?: SortType;
	selectable?: boolean;
	selected?: RowType[];
	activable?: boolean;
	rows?: RowType[];
	showEmpty?: boolean;
	fixColumns?: number;
	section?: Section | null;
	disableOn?: string | number | null;
}

const ATTRS: AttrCheck = {
	columnBorder: isBool,
	columnSize: isStr,
	columns: isArray,
	headLine: isBool,
	sortType: isStr,
	selectable: isBool,
	selected: isArray,
	activable: isBool,
	rows: isArray,
	showEmpty: isBool,
	fixColumns: isNumber,
	disableOn: (v) => isStr(v) || isNumber(v) || v === null,
	section: (v) => isObject(v) || v === null,
};

export function Table(props: TableProps, children: Nodes[]): UITable {
	return jsxBuild(new UITable(), props, children, ATTRS);
}
